// pages/login/index.js

var rx = require('../../utils/util.js')
var api = require('../../comm/api.js')

var app = getApp()
api.setApp(app)
Page({
  data: {
    name: null,
    userInfo: {
      avatarUrl: '../../../images/logo.png'
    }
  },
  onLoad: function () {
    var that = this
    wx.showNavigationBarLoading()
    app.getUserInfo(function (userInfo) {
      wx.hideNavigationBarLoading()
      console.log('获取用户信息')
      console.info(userInfo)
      if (api.user.isBind()) {
       that.navToHome()
        return;
      }

      that.setData({
        userInfo: userInfo
      });
    })
  },
  onNameChanged: function (e) {
    this.data.name = e.detail.value
  },
  navToHome:function(){
    wx.redirectTo({
      url: '/pages/home/index',
    })
  },
  authBind: function () {
    var that = this
    api.user.bind(this.data.name, function (res) {
      if (!res.success) {
        rx.alert(res.message)
        return
      }
      wx.showModal({
        content: '绑定成功，你当前登录的微信为：' + that.data.name,
        showCancel:false,
        success:function(){
          that.navToHome()
        }
      })
    });
  }
})
